package com.kt.common.collections

import java.util.*
import kotlin.collections.HashSet


fun main() {
    /*val numbers = mutableListOf("one", "two", "three", "four")
    numbers.add("five")
    numbers.println()*/
    //numbers = mutableListOf()//val cannot be reassigned


    /**
     * 集合中存储的值只能是唯一的
     * set：没有顺序
     * HshSet：无顺序，存储内存小
     * LinkedListSet :有顺序
     */
    /*val numbers = setOf(1, 2, 3, 4)
    println("Number of elements: ${numbers.size}")
    if (numbers.contains(1)) println("1 is in the set")

    val numbersBackwards = setOf(4, 3, 2, 1)
    println("The sets are equal: ${numbers == numbersBackwards}")*/ // set是集合是不带顺序的，顾此处会相等 ， LinkedHashSet 可以保证顺序

//    val mapOf = mapOf("zs" to 29, "ls" to 28, "ww" to 89)
//    mapOf.forEach{ println(it)}
    /*val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
    println("All keys: ${numbersMap.keys}")
    println("All values: ${numbersMap.values}")
    if ("key2" in numbersMap) println("Value by key \"key2\": ${numbersMap["key2"]}")
    if (1 in numbersMap.values) println("The value 1 is in the map")
    if (numbersMap.containsValue(1)) println("The value 1 is in the map") // 同上*/

    /*val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
    val anotherMap = mapOf("key2" to 2, "key1" to 1, "key4" to 1, "key3" to 3)

    println("The maps are equal: ${numbersMap == anotherMap}") //true 无论键值对的顺序如何，包含相同键值对的两个 Map 是相等的*/
    /*val numbersMap = mapOf("key1" to Per(name = "11"), "key2" to Per(name = "12"), "key3" to Per(name = "13"), "key4" to Per(name = "14"))
    val anotherMap = mapOf("key2" to Per(name = "12"), "key1" to Per(name = "11"), "key4" to Per(name = "14"), "key3" to Per(name = "13"))
    println("The maps are equal: ${numbersMap == anotherMap}") //里边是引用类型也会相等
    //numbersMap.put()  mapOf 不支持新增删除
    //mutableMapOf 底层是LinkedHashMap 是支持顺序的
    val mutableMapOf = mutableMapOf("key1" to Per(name = "11")) //MutableMap支持增删[] 或者put
    mutableMapOf["key8"] = Per(age = 12)
    mutableMapOf.put("key9", Per(age = 19))*/


    /**
     * 创建集合的方法： listOf<T>()、setOf<T>()、mutableListOf<T>()、mutableSetOf<T>()
     * 创建空集合：emptyList()、emptySet() 与 emptyMap()
     * */
    //list 的初始化函数
   /* val doubled = List(3) { it * 2 }  // 如果你想操作这个集合，应使用 MutableList
    println(doubled)

    //具体类型构造函数
    val linkedList = LinkedList<String>(listOf("one", "two", "three"))
    val presizedSet = HashSet<Int>(32)*/

    /**复制*/
    /*val sourceList = mutableListOf(1, 2, 3)
    val copyList = sourceList.toMutableList()
    val readOnlyCopyList = sourceList.toList()
    sourceList.add(4)
    //copyList.add(4)
    //readOnlyCopyList.add(4)//此处没有新增方法, 编译异常
    println("Copy size: ${copyList.size}")

    println("Read-only copy size: ${readOnlyCopyList.size}")

    //list构建set
    val copySet = sourceList.toMutableSet()
    copySet.add(3)//重复，只会出现一次
    copySet.add(4)
    println(copySet)*/

    //可以创建对同一集合实例的新引用。使用现有集合初始化集合变量时，将创建新引用。 因此，当通过引用更改集合实例时，更改将反映在其所有引用中
    /*val sourceList = mutableListOf(1, 2, 3)
    val referenceList = sourceList
    referenceList.add(4)
    println("Source size: ${sourceList.size}")*/

    //集合的初始化可用于限制其可变性 ，但是由于referenceList引用了sourceList 所以sourceList变化会带动referenceList变化
    /*val sourceList = mutableListOf(1, 2, 3)
    val referenceList: List<Int> = sourceList
    //referenceList.add(4)            // 编译错误
    sourceList.add(4)
    println(referenceList) // 显示 sourceList 当前状态*/

    //调用其他集合的函数
    //过滤
    /*val numbers = listOf("one", "two", "three", "four")
    val longerThan3 = numbers.filter { it.length > 3 }
    println(longerThan3)*/
    //
    /*val numbers = setOf(1, 2, 3)
    println(numbers.map { it * 3 })
    println(numbers.mapIndexed { idx, value -> value * idx })*/

    //集合生成map
    val numbers = listOf("one", "two", "three", "four")
    println(numbers.associateWith { it.length })
}


fun <T>MutableList<T>.println(){
    this.forEach{
        println(it)
    }
}
data class Per(var name: String = "s",var age:Int = 0)